package QC.MessageBox.service;

import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.ext.asyncsql.AsyncSQLClient;
import io.vertx.ext.asyncsql.MySQLClient;
import io.vertx.redis.RedisClient;
import io.vertx.redis.RedisOptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by Andaren on 2017/4/5.
 * Used for: MySQL连接数据源
 */
public class BaseJDBC {

    private static final Logger LOGGER = LoggerFactory.getLogger(BaseJDBC.class);
    private static AsyncSQLClient mySQLClient;
    private static RedisClient redisClient;
//    private static MongoClient mongoClient;

    private String host;
    private Integer port;
    private String username;
    private String password;
    private String database;
    private String charset;


    static {

    }

    /**
     * create shared asynchronized client
     */
    public static void createSharedClient(Vertx vertx, JsonObject config) {
        if (null == mySQLClient){
            // 设置连接池参数
            JsonObject mySQLClientConfig = new JsonObject()
                    .put("host", config.getString("mysql_host", "rdsduh28b0lh190n5igho.mysql.rds.aliyuncs.com"))
                    .put("port", config.getInteger("mysql_port", 3306))
                    .put("maxPoolSize", config.getInteger("mysql_max_pool_size", 10))
                    .put("database", config.getString("mysql_database", "test_shop"))
                    .put("charset", config.getString("mysql_charset", "UTF-8"))
                    .put("queryTimeout", config.getInteger("mysql_query_timeout", 10000))
                    .put("username", config.getString("mysql_username", "ds3"))
                    .put("password", config.getString("mysql_password", "3Ts7_0ed"));
            mySQLClient = MySQLClient.createShared(vertx, mySQLClientConfig);
            LOGGER.info(">>>连接池创建完成");
        }

        if (null == redisClient){
            // 设置连接池参数
            RedisOptions redisOptions = new RedisOptions()
                    .setHost(config.getString("redis_host", "120.25.88.91"))
                    .setAuth(config.getString("redis_auth", null))
                    .setPort(config.getInteger("redis_port", 6868));

            redisClient = RedisClient.create(vertx, redisOptions);
            LOGGER.info(">>>Redis连接创建完成");
        }

//        if (null == mongoClient) {
//            //MongoClientOptionsParser optionsParser = new MongoClientOptionsParser(config);
//            JsonObject configOption = new JsonObject();
//            configOption.put("db_name", config.getString("mongo_db_name", "message_box"))
//                    .put("useObjectId", config.getBoolean("mongo_useObjectId", true))
//                    .put("connection_string", config.getString("mongo_connection_string", "mongodb://120.25.88.91:27017"));//, "mongodb://120.25.88.91:27017"));
//            mongoClient = MongoClient.createNonShared(vertx, configOption);
//            LOGGER.info(">>>MongoDB连接创建完成");
//        }
    }

    public static AsyncSQLClient getSQLClient() {
        return mySQLClient;
    }

    public static RedisClient getRedisClient() {
        return redisClient;
    }

//    public static MongoClient getMongoClient() {return mongoClient;}
}
